/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main.java
 *
 * Created on May 15, 2011, 2:43:53 AM
 */
package uploadphotobucket;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import uploadphotobucket.view.AboutDialog;
import uploadphotobucket.view.HelpDialog;
import uploadphotobucket.view.LoginPanel;
import uploadphotobucket.view.UploadPanel;
import utils.Browser;
import utils.SharedObject;

/**
 *
 * @author banhbaochay
 */
public class Main extends javax.swing.JFrame {

    /** Creates new form Main */
    public Main() {
        changeUI("Nimbus");
        initData();
        initComponents();
        rs = ResourceBundle.getBundle("resourcebundle.Main", locale);
        addPanel("Log in", loginPanel, rs.getString("tooltip.login.tab"));
        addPanel("Upload", uploadPanel, rs.getString("tooltip.upload.tab"));
    }// end constructor

    // <editor-fold defaultstate="collapsed" desc="Change Look and Feel">
    private void changeUI(String LFName) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if (LFName.equalsIgnoreCase(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }
    }
    // </editor-fold>
    
    private void initData() {
        locale = Locale.ENGLISH;
        
        String oauthConsumerKey = null;
        String oauthConsumerSecret = null;
        try {
            oauthConsumerKey = ResourceBundle.getBundle("resourcebundle.consumer").getString("oauth.consumer");
            oauthConsumerSecret = ResourceBundle.getBundle("resourcebundle.consumer").getString("oauth.consumer.key");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Can't load data from ResourceBundle\nContact to author to fix bug");
            System.exit(0);
        }
        Browser browser = new Browser();
        String path = System.getProperty("user.home");
        sharedMap = new SharedObject(browser, oauthConsumerKey, oauthConsumerSecret);
        sharedMap.addPath(path);
        loginPanel = new LoginPanel(sharedMap, locale);
        uploadPanel = new UploadPanel(sharedMap, locale);
        aboutDialog = new AboutDialog(this, true, locale);
        helpDialog = new HelpDialog(this, true, locale);
    }// end initData method
    
    private void addPanel(String title, JPanel panel, String tooltip) {
        mainTab.addTab(title, null, panel, tooltip);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainTab = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        langMenu = new javax.swing.JMenu();
        engRadioBtn = new javax.swing.JRadioButtonMenuItem();
        vnRadioBtn = new javax.swing.JRadioButtonMenuItem();
        helpMenu = new javax.swing.JMenu();
        helpMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("resourcebundle/Main"); // NOI18N
        setTitle(bundle.getString("Main.title")); // NOI18N

        mainTab.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                mainTabStateChanged(evt);
            }
        });

        langMenu.setText(bundle.getString("Main.langMenu.text")); // NOI18N

        engRadioBtn.setSelected(true);
        engRadioBtn.setText(bundle.getString("Main.engRadioBtn.text")); // NOI18N
        engRadioBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                engRadioBtnActionPerformed(evt);
            }
        });
        langMenu.add(engRadioBtn);

        vnRadioBtn.setText(bundle.getString("Main.vnRadioBtn.text")); // NOI18N
        vnRadioBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vnRadioBtnActionPerformed(evt);
            }
        });
        langMenu.add(vnRadioBtn);

        jMenuBar1.add(langMenu);

        helpMenu.setText(bundle.getString("Main.helpMenu.text")); // NOI18N

        helpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, java.awt.event.InputEvent.CTRL_MASK));
        helpMenuItem.setText(bundle.getString("Main.helpMenuItem.text")); // NOI18N
        helpMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(helpMenuItem);

        aboutMenuItem.setText(bundle.getString("Main.aboutMenuItem.text")); // NOI18N
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTab, javax.swing.GroupLayout.DEFAULT_SIZE, 956, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainTab, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mainTabStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_mainTabStateChanged
        // TODO add your handling code here:
        JTabbedPane tab = (JTabbedPane) evt.getSource();
        if (tab.getSelectedIndex() == 1) {
            uploadPanel.reload();
        }// end if tab.getSelectedIndex() == 1
    }//GEN-LAST:event_mainTabStateChanged

    private void helpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpMenuItemActionPerformed
        // TODO add your handling code here:
        helpDialog.setVisible(true);
    }//GEN-LAST:event_helpMenuItemActionPerformed

    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        // TODO add your handling code here:
        aboutDialog.setVisible(true);
    }//GEN-LAST:event_aboutMenuItemActionPerformed

    private void engRadioBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_engRadioBtnActionPerformed
        // TODO add your handling code here:
            vnRadioBtn.setSelected(false);
            engRadioBtn.setSelected(true);
            locale = Locale.ENGLISH;
            changeLanguage();
            loginPanel.localeChanged(locale);
            loginPanel.changeLanguage();
            uploadPanel.localeChanged(locale);
            uploadPanel.changeLanguage();
            helpDialog.localeChanged(locale);
            helpDialog.changeLanguage();
            aboutDialog.localeChanged(locale);
            aboutDialog.changeLanguage();
    }//GEN-LAST:event_engRadioBtnActionPerformed

    private void vnRadioBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vnRadioBtnActionPerformed
        // TODO add your handling code here:
            engRadioBtn.setSelected(false);
            vnRadioBtn.setSelected(true);
            locale = new Locale("vi", "VN");
            changeLanguage();
            loginPanel.localeChanged(locale);
            loginPanel.changeLanguage();
            uploadPanel.localeChanged(locale);
            uploadPanel.changeLanguage();
            helpDialog.localeChanged(locale);
            helpDialog.changeLanguage();
            aboutDialog.localeChanged(locale);
            aboutDialog.changeLanguage();
    }//GEN-LAST:event_vnRadioBtnActionPerformed

    private void changeLanguage() {
        ResourceBundle rs = ResourceBundle.getBundle("resourcebundle.Main", locale);
        langMenu.setText(rs.getString("Main.langMenu.text"));
        helpMenu.setText(rs.getString("Main.helpMenu.text"));
        engRadioBtn.setText(rs.getString("Main.engRadioBtn.text"));
        vnRadioBtn.setText(rs.getString("Main.vnRadioBtn.text"));
        helpMenuItem.setText(rs.getString("Main.helpMenuItem.text"));
        aboutMenuItem.setText(rs.getString("Main.aboutMenuItem.text"));
        mainTab.setToolTipTextAt(0, rs.getString("tooltip.login.tab"));
        mainTab.setToolTipTextAt(1, rs.getString("tooltip.upload.tab"));
    }// end changeLanguage method
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JRadioButtonMenuItem engRadioBtn;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuItem helpMenuItem;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu langMenu;
    private javax.swing.JTabbedPane mainTab;
    private javax.swing.JRadioButtonMenuItem vnRadioBtn;
    // End of variables declaration//GEN-END:variables
    private SharedObject sharedMap;
    private LoginPanel loginPanel;
    private UploadPanel uploadPanel;
    private AboutDialog aboutDialog;
    private HelpDialog helpDialog;
    private Locale locale;
    ResourceBundle rs;
}// end Main class
